﻿//This file is Copyright © 2007 Tom Holden ( http://oxford.academia.edu/TomHolden )
//This file is part of the LogLinMapleCodeGen project.

//LogLinMapleCodeGen is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//LogLinMapleCodeGen is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with LogLinMapleCodeGen.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LogLinMapleCodeGen
{
    public partial class MapleCodeGenForm : Form
    {
        public MapleCodeGenForm( )
        {
            InitializeComponent( );
        }

        private void FloatingPointOption_CheckedChanged( object lSender, EventArgs lEventArgs )
        {
            DigitsTextBox.Enabled = FloatingPointOption.Checked;
            DigitsTextBox.TabStop = FloatingPointOption.Checked;
        }

        private void EvaluateAtOption_CheckedChanged( object lSender, EventArgs lEventArgs )
        {
            EvaluateAtTextBox.Enabled = EvaluateAtOption.Checked;
            EvaluateAtTextBox.TabStop = EvaluateAtOption.Checked;
        }

        private void SubmitButton_Click( object lSender, EventArgs lEventArgs )
        {
            Clipboard.Clear( );

            string lsPrefix = "";
            string lsSuffix = "";
            if( SimplificationOption.Checked )
            {
                lsPrefix = "simplify( ";
                lsSuffix = " )";
            }
            else if( SymbolicSimplificationOption.Checked )
            {
                lsPrefix = "simplify( ";
                lsSuffix = ", symbolic )";
            }
            else if( FloatingPointOption.Checked )
            {
                lsPrefix = "evalf( ";
                lsSuffix = ", " + DigitsTextBox.Text + " )";
            }
            else if( EvaluateAtOption.Checked )
            {
                lsPrefix = "eval( ";
                lsSuffix = ", " + EvaluateAtTextBox.Text + " )";
            }

            string lsEqualsString = " = ";
            string lsPlusString = " + ";
            if( SeperateComponentsCheckBox.Checked )
            {
                lsEqualsString = "; ";
                lsPlusString = "; ";
            }

            string lsClipboardString = "";
            string lsClipboardStringPrefix = "";
            string lsSolveLocString = "";
           
            foreach( string lsLine in FunctionSignatureTextBox.Lines )
            {
				if( lsLine.Length == 0 ) continue;

                lsClipboardString += "'" + lsLine + "'" + lsEqualsString;

                List<string> llsVariableNames = new List<string>( lsLine.Split( new Char[] { '(', ',', ' ', ')' }, StringSplitOptions.RemoveEmptyEntries ) );
                string lsFunctionName = llsVariableNames[0];
                llsVariableNames.RemoveAt( 0 );

                string lsVariableNames = "";
                string lsVariableNamesForEval = "";
                {
                    string lsComma = "";
                    foreach( string lsVariableName in llsVariableNames )
                    {
                        lsVariableNames += lsComma + lsVariableName;
                        lsVariableNamesForEval += lsComma + lsVariableName + " = " + lsVariableName + 's';
                        lsComma = ", ";
                    }
                }

                lsClipboardStringPrefix += "EvalLocFor_" + lsFunctionName + " := { " + lsVariableNamesForEval + " }; ";
                if( !NoSolvingOption.Checked )
                {
                    if( lsSolveLocString != "" ) lsSolveLocString += " union ";
                    lsSolveLocString += "EvalLocFor_" + lsFunctionName;
                }
				lsClipboardString += lsPrefix + "eval( eval( " + lsFunctionName + "( " + lsVariableNames + " ), " + "EvalLocFor_" + lsFunctionName + " ), EvalLocSteadyState )" + lsSuffix;

                foreach( string lsVariableName in llsVariableNames )
					lsClipboardString += lsPlusString + lsPrefix + "eval( eval( " + lsVariableName + " * diff( " + lsFunctionName + "( " + lsVariableNames + " ), " + lsVariableName + " ), " + "EvalLocFor_" + lsFunctionName + " ), EvalLocSteadyState ) * " + lsVariableName.ToLower( ) + 'd' + lsSuffix;

                lsClipboardString += ";\n";
            }

            if( !NoSolvingOption.Checked )
            {
				lsClipboardStringPrefix += "EvalLocSteadyState := ";
                if( FSolveOption.Checked ) lsClipboardStringPrefix += "f";
				lsClipboardStringPrefix += "solve( eval( { ";
				bool lbNotFirst = false;
				foreach( string lsLine in FunctionSignatureTextBox.Lines )
				{
					if( lsLine.Length == 0 ) continue;

					if( lbNotFirst ) lsClipboardStringPrefix += ", ";
					else lbNotFirst = true;
					lsClipboardStringPrefix += lsLine;
				}
				lsClipboardStringPrefix += " }, " + lsSolveLocString + " ) ); ";
			}
			else lsClipboardStringPrefix += "EvalLocSteadyState := { }; ";

            Clipboard.SetText( lsClipboardStringPrefix + lsClipboardString );
        }
    }
}
